package com.prs.crm.action.stock;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import bsh.This;

import com.prs.crm.domain.store.StorePlace;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeCommon;
import com.prs.crm.service.stock.StoreRoomService;
import com.prs.crm.service.sys.CodeCommonService;
import com.prs.crm.service.sys.CodeService;

public class StoreRoomAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private StoreRoomService storeRoomService;

	private PaginationSupport pagination;

	private StoreRoom room;

	private StorePlace place;

	private String manager;

	private Integer[] roomIds;
	private String state = "c";

	private CodeCommon codeCommon;
	private CodeCommonService codeCommonService;
	private String Encode;
	private Code code;
	private CodeService codeService;
	private Collection<CodeCommon> codelist;

	public void setStoreRoomService(StoreRoomService storeRoomService) {
		this.storeRoomService = storeRoomService;
	}

	public StoreRoomService getStoreRoomService() {
		return storeRoomService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setRoom(StoreRoom room) {
		this.room = room;
	}

	public StoreRoom getRoom() {
		return room;
	}

	public void setRoomIds(Integer[] roomIds) {
		this.roomIds = roomIds;
	}

	public Integer[] getRoomIds() {
		return roomIds;
	}

	public void setPlace(StorePlace place) {
		this.place = place;
	}

	public StorePlace getPlace() {
		return place;
	}

	public void setManager(String manager) {
		this.manager = manager;
	}

	public String getManager() {
		return manager;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	@Action("listStoreRoomChange")
	public String listStoreRoomChange() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				StoreRoom.class);
		this.setPagination(this.getStoreRoomService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("listStoreRoom")
	public String listStoreRoom() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				StoreRoom.class);
		this.setPagination(this.getStoreRoomService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editStoreRoom")
	public String editStoreRoom() {
		if (!state.equals("b")) {
			if (room != null)
				state = "a";
		}
		
		this.codelist = this.getCodeCommonService().getCodeCommonByCodeType(
				"库房");

		return SUCCESS;
	}

	@Action("viewStoreRoom")
	public String viewStoreRoom() {
		return SUCCESS;
	}

	@Action(value = "saveStoreRoom", results = {
			@Result(name = "success", type = "redirect", location = "listStoreRoom"),
			@Result(name = "error", type = "chain", location = "editStoreRoom") })
	public String saveStoreRoom() {
		
		if (room.getName() == null || room.getName().equals("")) {
			state = "b";
			this.addActionError("名称不能为空");
			return ERROR;
		}
		
		if (room.getRoomcode() == null || room.getRoomcode().equals("")) {
			state = "b";
			this.addActionError("编码不能为空");
			return ERROR;
		}
		
		if (manager == null || manager.equals("")) {
			state = "b";
			this.addActionError("库管员不能为空");
			return ERROR;
		}
		
		if (room.getId() != null) {
			for (StoreRoom storeRoom : this.getStoreRoomService().getAllstoreroom()) {
				if (storeRoom.getName().trim().equals(room.getName().trim())&&room.getId()!=storeRoom.getId()) {
					state = "b";
					this.addActionError("名称已经存在");
					return ERROR;
				}
			}
			this.getStoreRoomService().save(room, manager);
		} else {
			for (StoreRoom storeRoom : this.getStoreRoomService().getAllstoreroom()) {
				if (storeRoom.getName().trim().equals(room.getName().trim())) {
					state = "b";
					this.addActionError("名称已经存在");
					return ERROR;
				}
			}
			if ("x".equals(Encode) || "".equals(Encode)) {
				this.addActionError("请选择编码类型");
				return ERROR;
			}
			if ("n".equals(Encode)) {
				int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
						"库房", room.getRoomcode().trim());
				
				if (EncodeRplice != 0) {
					this.addActionError("编码重复!请重新输入");
					return ERROR;
				}
			}
			if ("n".equals(Encode)) {
				Code codes = new Code();
				codes.setCodeValue(room.getRoomcode());
				codes.setEntityName(this.getClass().getName());
				codes.setCodeType("手动编码");
				codes.setCodeMolde("库房");
				this.getCodeService().save(codes);
				room.setCode(codes);
				this.getStoreRoomService().save(room, manager);
			} else {
				CodeCommon Vob = this.getCodeCommonService().getVobject(
						codeCommon.getId());
				Code codes = new Code();
				codes.setEntityName(this.getClass().getName());
				codes.setCodeValue(room.getRoomcode());
				Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
				this.codeCommonService.saveOrUpdate(Vob);
				codes.setCodeType(Vob.getCodePlan());
				codes.setCodeMolde("库房");
				this.getCodeService().save(codes);
				room.setCode(codes);
				this.getStoreRoomService().save(room, manager);
			}
		}

		return SUCCESS;
	}

	@Action(value = "removeStoreRoom", results = { @Result(name = "success", type = "redirect", location = "listStoreRoom") })
	public String removeStoreRoom() {
		this.getStoreRoomService().batchRemove(roomIds);
		return SUCCESS;
	}

	@Action("editStorePlace")
	public String editStorePlace() {
		return SUCCESS;
	}

	@Action(value = "saveStorePlace", results = { @Result(name = "success", type = "redirect", location = "editStoreRoom?room=${room.id}") })
	public String saveStorePlace() {
		this.getStoreRoomService().saveStorePlace(room, place);
		return SUCCESS;
	}

	@Action(value = "removeStorePlace", results = { @Result(name = "success", type = "redirect", location = "editStoreRoom?room=${room.id}") })
	public String removeStorePlace() {
		this.getStoreRoomService().removeStorePlace(room, place);
		return SUCCESS;
	}

	public CodeCommon getCodeCommon() {
		return codeCommon;
	}

	public void setCodeCommon(CodeCommon codeCommon) {
		this.codeCommon = codeCommon;
	}

	public CodeCommonService getCodeCommonService() {
		return codeCommonService;
	}

	public void setCodeCommonService(CodeCommonService codeCommonService) {
		this.codeCommonService = codeCommonService;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeCommon> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeCommon> codelist) {
		this.codelist = codelist;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}

}
